const { app, BrowserWindow, Menu, ipcMain } = require("electron");
const path = require("path");
const isDev = require("electron-is-dev");
const menuTemplate = require("./src/utils/menuTemplate");
const AppWindow = require("./src/utils/AppWindow");

function createWindow() {
  const urlLocaltion = isDev ? "http://localhost:3008" : "";
  const mainConfig = { width: 1150, height: 700 };
  mainWindow = new AppWindow(mainConfig, urlLocaltion);
  mainWindow.on("closed", () => {
    mainWindow = null;
  });
  // enable remote modules
  require("@electron/remote/main").initialize();
  require("@electron/remote/main").enable(mainWindow.webContents);
  // set the main menu
  let menu = Menu.buildFromTemplate(menuTemplate);
  Menu.setApplicationMenu(menu);

  // set setting window
  ipcMain.on("open-settings-window", () => {
    const settingConfig = {
      width: 520,
      height: 450,
      parent: mainWindow,
    };
    const settingLocation = `file://${path.join(
      __dirname,
      "./settings/settings.html"
    )}`;
    settingWindow = new AppWindow(settingConfig, settingLocation);
    settingWindow.on("closed", () => {
      settingWindow = null;
    });
    require("@electron/remote/main").enable(settingWindow.webContents);
  });
}

app.whenReady().then(() => {
  createWindow();

  app.on("activate", () => {
    if (BrowserWindow.getAllWindows().length === 0) {
      createWindow();
    }
  });
});
